package com.naiterui.ehp.ps.push.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.utils.thread.ThreadPoolUtil;
import com.naiterui.ehp.ps.push.service.ITaskRecordService;
import com.naiterui.ehp.ps.push.service.handler.UserPushHandler;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * push发送控制类
 *
 * @author chenlin modify by dongiqng
 * @since 1.0.0
 */
@Controller
@RequestMapping("push/taskRecord")
@Api(tags = {SwaggerTag.PUSH + SwaggerTag.PUSH_MESSAGE})
public class TaskRecordController {

    private static Logger logger = LoggerFactory.getLogger(TaskRecordController.class);

    @Autowired
    private ITaskRecordService taskRecordService;
    @Autowired
    private UserPushHandler userPushHandler;

    /**
     * 每次检索失败记录默认记录数
     */
    private static final Integer FETCH_FAILRECORD_DEFAULT_SIZE = 50;

    @ResponseBody
    @RequestMapping("remove")
    public void remove() {
        logger.info("start remove completed push task record ...");
        this.taskRecordService.removeCompleted();
        logger.info("end remove completed push task record ...");
    }

    @RequestMapping("handleStatus")
    public @ResponseBody
    void handleStatus() {
        logger.info("start handle im push task status ands remove source push");
        this.taskRecordService.handleStatus();
        logger.info("end handle im push task status ands remove source push");
    }

    /**
     * 补发,定时任务调用
     *
     * @return
     */
    @RequestMapping("push/resend")
    @ResponseBody
    public void reSendFailedPush(Integer batchSize) {
        final Integer querySize = batchSize == null ? FETCH_FAILRECORD_DEFAULT_SIZE : batchSize;
        ThreadPoolUtil.execute(() -> {
            /*
             补发android
             */
            TaskRecordController.this.taskRecordService.reSendFailedPush(querySize);

            /*
              补发ios
             */
            TaskRecordController.this.userPushHandler.reSendPushByGroup();
        });
    }
}
